import { ElMessage } from "element-plus";
import { createRouter, createWebHistory } from "vue-router";
import { useUserStore } from "../stores/userStore";

import Home from "../views/home/Home.vue";
import DetailView from "../views/detail/DetailView.vue";
import About from "../views/about/About.vue";
import Archives from "../views/archives/Archives.vue";
import Front from "../views/front/Front.vue";
import Back from "../views/back/Back.vue";
import Life from "../views/life/Life.vue";
import Admin from "../views/admin/Admin.vue";
import Layout from "../views/layout/Layout.vue";
import SiteState from "../views/state/SiteState.vue";
import CategoryTimeLine from "../views/state/CategoryTimeLine.vue";
import TagTimeLine from "../views/state/TagTimeLine.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "layout",
      component: Layout,
      meta: {
        title: "向往每一天",
      },
      children: [
        {
          path: "",
          name: "home",
          component: Home,
          meta: {
            isAuth: false,
          },
        },
        {
          path: "/detail/:id",
          name: "detailView",
          component: DetailView,
        },
        {
          path: "/archives",
          name: "archives",
          component: Archives,
        },
        {
          path: "/about",
          name: "about",
          component: About,
        },
        {
          path: "/front",
          name: "front",
          component: Front,
        },
        {
          path: "/back",
          name: "back",
          component: Back,
        },
        {
          path: "/life",
          name: "life",
          component: Life,
        },
        {
          path: "/state/:type",
          name: "state",
          component: SiteState,
        },
        {
          path: "/category/:id",
          name: "categoryTimeLine",
          component: CategoryTimeLine,
        },
        {
          path: "/tag/:id",
          name: "tagTimeLine",
          component: TagTimeLine,
        },
      ],
    },

    {
      path: "/admin",
      component: Admin,
      meta: {
        isAuth: true,
        title: "管理端",
      },
      children: [
        {
          path: "",
          name: "admin",
          component: () => import("../views/admin/components/Article.vue"),
        },
        {
          path: "article",
          name: "article",
          component: () => import("../views/admin/components/Article.vue"),
        },
        {
          path: "category",
          name: "category",
          component: () => import("../views/admin/components/Category.vue"),
        },
        {
          path: "tag",
          name: "tag",
          component: () => import("../views/admin/components/Tag.vue"),
        },
        {
          path: "setting",
          name: "setting",
          component: () => import("../views/admin/components/Setting.vue"),
        },
      ],
    },
    {
      path: "/edit",
      name: "edit",
      component: () => import("../components/Edit.vue"),
      meta: {
        title: "努力完成每一篇文章",
      },
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/login/Login.vue"),
      meta: {
        title: "向往每一天",
      },
    },
    {
      path: "/404",
      name: "404",
      component: () => import(["../views/error/NotFound.vue"]),
      meta:{
        title: '页面不见了'
      },
    },
    {
      path: "/:pathMatch(.*)*",
      redirect: {
        path: "/404",
      },
    },
  ],
  scrollBehavior() {
    return {
      top: 0,
    };
  },
});

// 配置路由导航
router.beforeEach((to, from, next) => {
  // 动态修改网站标题
  if(to.meta.title) {
    document.title = to.meta.title
  }
  // 判断资源是否要拦截
  if (to.matched.some((route) => route.meta.isAuth)) {
    // 如果资源要拦截, 判断是否有token
    const token = useUserStore().token;
    // 如果token存在, 直接放行
    if (token) {
      next();
    } else {
      // 没有token 跳转登录页
      ElNotification({
        title: "Warning",
        message: "请先登录",
        type: "warning",
      });
      next({
        path: "/login",
      });
    }
  } else {
    // 请求的资源不需要拦截 直接放行
    next();
  }
});

export default router;
